﻿using System;
using System.Linq;
using System.Collections.Generic;

using TempEmployees.DAL;
using TempEmployees.BLL.Database;

namespace TempEmployees.BLL.EntitiesExtensions
{
    public static class EDepartmentInfo 
    {
        public static List<DepartmentsInfo> GetDepartmentsInfo(int semesterId)
        {
            return DbQuery.Database.DepartmentsInfos.Where(di => di.SemesterID == semesterId).ToList();
        }

        public static DepartmentsInfo GetDepartmentInfo(int departmentId, int semesterId)
        {
            var departmentInfo = DbQuery.Database.DepartmentsInfos.Where((di) => di.DepartmentID == departmentId && di.SemesterID == semesterId);
            if (departmentInfo.Count() == 1) return departmentInfo.First();
            DepartmentsInfo newInfo = new DepartmentsInfo(departmentId, semesterId);
            DbQuery.Insert(newInfo);
            return newInfo;
        }

        private static DepartmentsInfo GetDepartmentInfo(int id)
        {
            return DbQuery.SelectByPK<DepartmentsInfo>(id.ToString());
        }

        public static bool Update(DepartmentsInfo info)
        {
            try
            {
                DbQuery.Update<DepartmentsInfo>(info, GetDepartmentInfo(info.ID));
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
